class CourseMaterial < ActiveRecord::Base
    #validates_presence_of :title
    
    
    def uploaded_document=(document_field)
      self.filename      = base_part_of(document_field.original_filename)
      #self.title         = "my title"
      #self.document_url  = "some url"
      self.content_type  = document_field.content_type.chomp
      self.document      = document_field.read
    end

    def base_part_of(file_name)
      File.basename(file_name).gsub(/[^\w._-]/, '')
    end
    
    protected
    	def self.find_all_materials
    		find(:all, :conditions => ["doc_type = 3"], :order => "title")
    	end
    	
    	def self.find_last_material
    		find(:first, :order => "created_at desc")
    	end
    	
    	def self.delete_old_schedule
    	   cms = find(:all, :conditions => ["doc_type = 2"])
    	   if cms.size > 1
      	     cm = find(:first, :conditions => ["doc_type = 2"], :order => "created_at")
    	     destroy(cm.id);
    	   end
    	end
    	
    	def self.delete_old_syllabus
    	   cms = find(:all, :conditions => ["doc_type = 1"])
    	   if cms.size > 1
      	     cm = find(:first, :conditions => ["doc_type = 1"], :order => "created_at")
        	 destroy(cm.id);
      	   end
    	end
    	
    	def self.delete_old_logo
    	   cms = find(:all, :conditions => ["doc_type = 0"])
    	   if cms.size > 1
       	    cm = find(:first, :conditions => ["doc_type = 0"], :order => "created_at")
      	    destroy(cm.id);
       	   end
    	end
    
end
